package lovely.gwt.json.rebind;

import rocket.generator.rebind.codeblock.TemplatedCodeBlockException;
import rocket.generator.rebind.codeblock.TemplatedFileCodeBlock;
import rocket.generator.rebind.type.Type;
import rocket.util.client.Checker;

/**
 * An abstraction for the read-json template
 * 
 * @author Stefan Martin
 */
public class ReadJsonTemplatedFile extends TemplatedFileCodeBlock {

	public ReadJsonTemplatedFile() {
		super();
	}

	/**
	 * The type being deserialized
	 */
	private Type deserializerType;

	protected Type getDeserializerType() {
		Checker.notNull("deserializerType:deserializerType", deserializerType);
		return this.deserializerType;
	}

	public void setDeserializerType(final Type deserializerType) {
		Checker.notNull("parameter:deserializerType", deserializerType);
		this.deserializerType = deserializerType;
	}

	@Override
	protected String getResourceName() {
		return ReadJsonConstants.TEMPLATE;
	}

	@Override
	protected Object getValue0(final String name) {
		Object value = null;
		while (true) {
			if (ReadJsonConstants.TYPE.equals(name)) {
				value = this.getDeserializerType();
				break;
			}
			break;
		}
		return value;
	}

	protected void throwValueNotFoundException(final String name) {
		throw new TemplatedCodeBlockException("Value for placeholder \"" + name + "\" not found in file \"" + ReadJsonConstants.TEMPLATE + "\".");
	}
}
